<template>
  <BasicModal
    v-bind="$attrs"
    @register="registerModal"
    :title="displayModuleTitle"
    width="1100px"
    centered
    minHeight="310"
    :footer="null"
    @cancel="handleCancel"
  >
    <BasicTable
      :data-source="tableData"
      @register="registerTable"
      :pagination="false"
      size="small"
      :scroll="{ x: false, y: '250px' }"
    >
      <template #toolbar>
        <a-button type="success" @click="onAddExchangeDataButtonClick"> <PlusOutlined /></a-button>
      </template>
      <template #execresult="{ record }">
        <a-tooltip v-if="(record.execresult & 4) == 4">
          <template #title>取消</template>
          <StopOutlined style="color: orange; font-size: 15px" />
        </a-tooltip>
        <a-tooltip v-else-if="(record.execresult & 1) == 0">
          <template #title>未执行</template>
          <LoginOutlined style="font-size: 15px" />
        </a-tooltip>
        <a-tooltip v-else-if="(record.execresult & 2) == 2">
          <template #title>执行成功</template>
          <CheckCircleOutlined style="color: green; font-size: 15px" />
        </a-tooltip>
        <a-tooltip v-else>
          <template #title>执行失败</template>
          <ExclamationCircleOutlined style="color: red; font-size: 15px" />
        </a-tooltip>
      </template>
      <template #action="{ column, record }">
        <TableAction
          v-if="(record.execresult & 4) !== 4 && (record.execresult & 1) == 0"
          :actions="[
            {
              color: 'error',
              label: '取消',
              popConfirm: {
                title: '是否确认取消',
                placement: 'left',
                confirm: onCancelExchangeDataButtonClick.bind(null, record),
              },
            },
          ]"
        />
      </template>
    </BasicTable>
    <AddOrUpdateExchangeData
      @register="registerAddOrUpdateExchangeDataModal"
      @success="afterSuccess"
    />
  </BasicModal>
</template>
<script lang="ts">
  import { defineComponent, onMounted, ref, computed, unref } from 'vue';
  import { BasicTable, useTable, TableAction } from '/@/components/Table';
  import { BasicModal, useModalInner, useModal } from '/@/components/Modal';
  import { queryEnumOptions } from '/@/api/platform/enum';
  import { displayTitle } from '/@/utils/QcUnifyUtil';
  import { QcUnifyTransToChildProps } from '/#/QcUnify';
  import { OperationTypeEnum } from '/@/enums/appEnum';
  import { useMessage } from '/@/hooks/web/useMessage';
  import AddOrUpdateExchangeData from './AddOrUpdateExchangeData.vue';
  import { cancelExchangeDataOperation, queryExchangeDataLogs } from '/@/api/qms/exchangeData';
  import { exchangeDataLogColumns, exchangeDataLogCondition } from './exchangeData.data';
  const { createMessage } = useMessage();
  import {
    PlusOutlined,
    StopOutlined,
    CheckCircleOutlined,
    ExclamationCircleOutlined,
    LoginOutlined,
  } from '@ant-design/icons-vue';
  export default defineComponent({
    components: {
      BasicModal,
      BasicTable,
      TableAction,
      AddOrUpdateExchangeData,
      PlusOutlined,
      StopOutlined,
      CheckCircleOutlined,
      ExclamationCircleOutlined,
      LoginOutlined,
    },
    name: 'ExchangeDataLog',
    emits: ['success'],
    setup(props, { emit }) {
      const qcUnifyProps = ref<QcUnifyTransToChildProps>();
      // 查询静态信息的单元id
      const cid = ref();
      const stcd = ref();
      const stcdOption = ref();
      const tableData = ref([]);
      const [
        registerAddOrUpdateExchangeDataModal,
        { openModal: openAddOrUpdateExchangeDataModal },
      ] = useModal();
      const [registerTable, { getForm, reload }] = useTable({
        api: queryExchangeDataLogs,
        immediate: false,
        columns: exchangeDataLogColumns,
        canResize: true,
        striped: true,
        showTableSetting: true,
        showIndexColumn: true,
        isTreeTable: false,
        bordered: true,
        ellipsis: true,
        indexColumnProps: {
          width: 50,
        },
        beforeFetch: (params) => {
          params.stcd = stcd.value;
          params.cid = cid.value;
          return params;
        },
        useSearchForm: true,
        formConfig: {
          labelWidth: 120,
          schemas: exchangeDataLogCondition,
          showResetButton: false, //查询条件部分中是否显示重置按钮
          fieldMapToTime: [
            // data为时间组件在表单内的字段，startTime，endTime为转化后的开始时间与结束时间
            // 'YYYY-MM-DD'为时间格式，参考moment
            ['tm', ['begintm', 'endtm'], 'YYYY-MM-DD HH:mm:ss'],
            // 支持多个字段
          ],
        },
        actionColumn: {
          width: 60,
          title: '操作',
          dataIndex: 'action',
          slots: { customRender: 'action' },
          fixed: 'right', //表格操作列是否固定，不设置时默认固定在右侧
        },
      });
      const [registerModal, { closeModal }] = useModalInner((data) => {
        if (data != undefined && data != null) {
          let unrefData = unref(data);
          qcUnifyProps.value = unrefData;
          //接收父组件传递的数据
          if (unrefData?.cid) {
            cid.value = unrefData.cid;
            stcd.value = unrefData.stcd;
            stcdOption.value = unrefData.stcdOption;
            reload();
          }
        }
      });

      /**
       * 打开新增窗口
       */

      function onAddExchangeDataButtonClick() {
        //构造父组件给子组件传值对象
        let propsData: QcUnifyTransToChildProps = {
          moduleName: '人工操作',
          operateType: OperationTypeEnum.ADD,
          cid: cid.value,
          stcd: stcd.value,
          stcdOption: stcdOption.value,
        };
        openAddOrUpdateExchangeDataModal(true, propsData);
      }

      /**
       * 取消
       * @param record
       */
      function onCancelExchangeDataButtonClick(record: EhsExchangeDataDto) {
        cancelExchangeDataOperation(record.logid)
          .then((res) => {
            console.log('res', res);
            //res判断结果：同时==undefined和null
            if (res == undefined && (res == null || Object.keys(res).length == 0)) {
              createMessage.success('取消成功。', 2);
              afterSuccess();
            }
          })
          .catch((ex) => {
            console.log('catch ', ex);
            createMessage.error(ex.msg);
          });
      }
      /**
       * 弹窗确认操作之后刷新页面
       */
      function afterSuccess() {
        reload();
      }
      let displayModuleTitle = computed(() => {
        return displayTitle(qcUnifyProps.value);
      });

      function handleCancel() {
        closeModal();
        emit('success');
      }
      onMounted(() => {});
      return {
        registerModal,
        registerTable,
        registerAddOrUpdateExchangeDataModal,
        closeModal,
        afterSuccess,
        tableData,
        displayModuleTitle,
        onAddExchangeDataButtonClick,
        onCancelExchangeDataButtonClick,
        handleCancel,
      };
    },
  });
</script>
<style scoped>
  :deep(.vben-basic-table-form-container .ant-form) {
    padding: 0;
    margin-bottom: 0;
  }
</style>
